package com.wzh.glsc.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.Query;
import com.wzh.glsc.product.dao.SkuInfoDao;
import com.wzh.glsc.product.entity.SkuImagesEntity;
import com.wzh.glsc.product.entity.SkuInfoEntity;
import com.wzh.glsc.product.entity.SpuInfoDescEntity;
import com.wzh.glsc.product.service.*;
import com.wzh.glsc.product.vo.SkuItemSaleAttrVo;
import com.wzh.glsc.product.vo.SkuItemVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
    @Resource
    SkuImagesService skuImagesService;
    @Resource
    SkuSaleAttrValueService skuSaleAttrValueService;
    @Resource
    SpuInfoDescService spuInfoDescService;
    @Resource
    AttrGroupService attrGroupService;
    @Resource
    ThreadPoolExecutor executor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        baseMapper.insert(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByParams(Map<String, Object> params) {
        String key=(String)params.get("key");
        QueryWrapper<SkuInfoEntity> skuInfoEntityQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty((String) params.get("brandId"))&&!"0".equals((String) params.get("brandId"))){
            skuInfoEntityQueryWrapper.eq("brand_id", (String)params.get("brandId"));
        }
        if(StringUtils.isNotEmpty((String) params.get("catelogId"))&&!"0".equals((String) params.get("catelogId"))){
            skuInfoEntityQueryWrapper.eq("catalog_id", (String)params.get("catelogId"));
        }
        if(StringUtils.isNotEmpty((String) params.get("min"))){
            try {
                BigDecimal min=new BigDecimal((String)params.get("min"));
                if(!min.equals(new BigDecimal(0))){
                    skuInfoEntityQueryWrapper.ge("price", min);
                }
            }catch (Exception e){

            }
        }
        if(StringUtils.isNotEmpty((String) params.get("max"))){
            try {
                BigDecimal max=new BigDecimal((String)params.get("max"));
                if(!max.equals(new BigDecimal(0))){
                    skuInfoEntityQueryWrapper.le("price", max);
                }
            }catch (Exception e){

            }
        }
        if(StringUtils.isNotEmpty(key)){
            skuInfoEntityQueryWrapper.and((obj) -> {
                obj.like("sku_id",key).or().like("sku_name", key)
                        .or().like("sku_title", key)
                        .or().like("sku_subtitle", key)
                        .or().like("price", key);
            });
        }
        IPage<SkuInfoEntity> skuInfoEntityIPage=this.page(new Query<SkuInfoEntity>().getPage(params),skuInfoEntityQueryWrapper);
        return new PageUtils(skuInfoEntityIPage);
    }

    @Override
    public List<SkuInfoEntity> getSkuBySpuId(Long spuId) {
       return baseMapper.selectList(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
    }

    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo=new SkuItemVo();
        //sku基本信息 pms_sku_info
        CompletableFuture<SkuInfoEntity> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity info =this.getById(skuId);
            skuItemVo.setInfo(info);
            return info;
        }, executor);


        //spu销售属性
        CompletableFuture<Void> saleFuture = skuInfoFuture.thenAcceptAsync(res -> {
            List<SkuItemSaleAttrVo> saleAttrVos = skuSaleAttrValueService.getSaleAttrBySpuId(res.getSpuId());
            skuItemVo.setSaleAttr(saleAttrVos);
        }, executor);

        //spu介绍
        CompletableFuture<Void> spuInfoFuture = skuInfoFuture.thenAcceptAsync(res -> {
            SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(res.getSpuId());
            skuItemVo.setDesc(spuInfoDescEntity);
        }, executor);
        //spu规格参数
        CompletableFuture<Void> attrGroupFuture = skuInfoFuture.thenAcceptAsync(res -> {
            List<SkuItemVo.SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());
            skuItemVo.setGroupAttrs(attrGroupVos);
        }, executor);

        //sku 图片信息  pms_sku_images
        CompletableFuture<Void> skuImagesFuture = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> imagesEntities = skuImagesService.getImagesBySkuId(skuId);
            skuItemVo.setImages(imagesEntities);
        }, executor);

        //这里还可以用.exceptionally()来处理异常
        //这里要用get()，不然主线程不会等待
        CompletableFuture.allOf(skuInfoFuture,saleFuture,spuInfoFuture,
                attrGroupFuture,skuImagesFuture).get();

        return skuItemVo;
    }

    @Override
    public BigDecimal priceBySkuId(Long skuId) {
        return this.getById(skuId).getPrice();
    }

}